package gui;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JOptionPane;

import functions.MainProgram;
import functions.PaymentData;
public class Gui {

	
	private static final String DIR = "c:\\A_joe_paymentInfo";
	private static final String FILE = DIR + "\\payment.dat";

	/**
	 * @param args
	 */
	public static void main(String[] args){
		createDir();
		try{
			
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(FILE));
			PaymentData pd = (PaymentData) in.readObject();
			in.close();
			MainProgram mp = new MainProgram(pd, FILE);
			mp.run();
			writeData(pd);
			System.out.println("file exists");
		}catch(Exception e){
			JOptionPane.showMessageDialog(null, "Since this is your first time using this program\n" + 
					"You will need to enter some information.");
			double beginningBalance = Double.parseDouble(
					JOptionPane.showInputDialog("Enter the total balance owed before any payements: "));
			PaymentData pd = new PaymentData();
			pd.setBeginningBalance(beginningBalance);
			MainProgram mp = new MainProgram(pd, FILE);
			mp.run();
			writeData(pd);
			
		}
		
	}

	private static void createDir() {
		
		File f = new File(DIR);
		if(!f.exists()){
			try {
				f.mkdirs();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				JOptionPane.showMessageDialog(null, "Error initiating directory");
			}
		}
		
		
	}

	public static void writeData(PaymentData pd){
		try{
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE));
			out.writeObject(pd);
			out.close();
		}catch(Exception e){
			JOptionPane.showMessageDialog(null, "An error occurred while trying to close.");
		}
	}
}
